package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.pojo.Result;
import com.neuedu.pojo.TBed;
import com.neuedu.mapper.TBedMapper;
import com.neuedu.pojo.TElder;
import com.neuedu.service.ITBedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2024-06-17
 */
@Service
public class TBedServiceImpl extends ServiceImpl<TBedMapper, TBed> implements ITBedService {
    @Autowired
    TBedMapper bedMapper;

    @Override
    public Result<List<TBed>> getPage(Integer currentPage, Integer pageSize, String bedNumber, Integer bedStatus) {
        Page<TBed> page = new Page<>(currentPage, pageSize);
        // 设置条件查询的构造器
        LambdaQueryWrapper<TBed> wrapper = new LambdaQueryWrapper<>();
        // 若条件为null，这个相当于全部查询
        wrapper.eq(StringUtils.isNotBlank(bedNumber), TBed::getBedNumber, bedNumber)
                .eq(bedStatus != null, TBed::getBedStatus, bedStatus);
        bedMapper.selectPage(page, wrapper);
        return Result.success(page.getRecords(), "查询成功", page.getTotal());
    }

    @Override
    public Result addBed(TBed bed) {
        bed.setBedId(null);
        return Result.success(bedMapper.insert(bed),"添加成功");
    }

    @Override
    public Result modStatusByBedId(Integer bedId) {
        LambdaUpdateWrapper<TBed> luw = new LambdaUpdateWrapper<>();
        luw.eq(TBed::getBedId, bedId)
                .set(TBed::getBedStatus, 1);
        return Result.success(bedMapper.update(null, luw));
    }

    @Override
    public Result deletebed(Integer bedId) {
        LambdaUpdateWrapper<TBed> wrapper =new LambdaUpdateWrapper<>();
        wrapper.eq(TBed::getBedId,bedId);
        return Result.success(bedMapper.delete(wrapper),"删除成功");
    }
}
